package util;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.google.common.io.Files;

/**
 * Utility class for most common reusable functions for file
 *
 * @author pradeeph
 *
 */
public class FileUtil
{

    /**
     * get the path of project root
     *
     * @return
     */
    public static String getProjectRoot()
    {
        String path = "";

        try
        {
            path = (new File(".")).getCanonicalPath();
        }
        catch (IOException e)
        {
            path = "";
        }

        return path;
    }

    /**
     * get the path of doc-upload directory path
     *
     * @return
     */
    public static String getDocUploadDirectoryPath()
    {
        return getProjectRoot() + File.separatorChar + "resource" + File.separatorChar + "doc-upload";
    }


    /**
     * get the list of files with selected file extensions
     *
     * @param dirName
     * @param fileExtensions
     * @return
     */
    public static File[] listFilesInDirectory(String dirName,String ...fileExtensions)
    {
        final List<String> fileExtensionList = Arrays.asList(fileExtensions);
        File dir = new File(dirName);

        return dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename)
            {
                return fileExtensionList.contains(Files.getFileExtension(filename.toLowerCase()));
            }
        });

    }

}
